package dao

import (
	"broker-server/internal/data/user"
	"broker-server/internal/database"
	gorms "broker-server/internal/database/gorm"
	"context"
	"gorm.io/gorm"
)

type UserDao struct {
	conn *gorms.GormConn
}

func NewUserDao() *UserDao {
	return &UserDao{
		conn: gorms.New(),
	}
}
func (d *UserDao) SaveUser(conn database.DbConn, ctx context.Context, user *user.User) error {
	d.conn = conn.(*gorms.GormConn)
	return d.conn.Tx(ctx).Create(user).Error
}

func (d *UserDao) FindUser(ctx context.Context, username, password string) (error, *user.User) {
	user := &user.User{}
	err := d.conn.Session(ctx).Where("user_name =? and pass_word=?", username, password).First(user).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	return err, user
}
